package academic.emailClient.persistence;

import academic.emailClient.model.entities.Message;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.*;
import org.apache.lucene.util.Version;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class IndexTheMessage {

	private final String DATE_FORMAT = "yyMMddHHmmss"; 
	private IndexWriter indexWriter;
	private final String INDEX_FOLDER=".\\Email Folders Index";
	/**
	 * this method offer the incremental indexing in the already created index file. So, it indexes only 
	 * the new created mail  or that have being moved to other folder 
	 *  
	 * @param message
	 * @param path
	 * @throws IOException
	 */

	public synchronized void index(Message message,String path){				
		//initialization
		File indexStorage=new File(INDEX_FOLDER);
		try {
				indexWriter = new IndexWriter(
					FSDirectory.open(indexStorage),
					new StandardAnalyzer(Version.LUCENE_CURRENT),
					false,
					IndexWriter.MaxFieldLength.LIMITED);
					Document doc = new Document();
		   //indexing the message
			 try{
				 DateFormat df = new SimpleDateFormat(DATE_FORMAT);
				 String dateWithPlainFormat = df.format(message.getDate());
				 String messagePath = path + File.separator + message.getId().toString();
				 doc.add(new Field("FROM",message.getFrom(),Field.Store.YES,Field.Index.NOT_ANALYZED));
				 doc.add(new Field("TO",message.getTo(),Field.Store.YES,Field.Index.NOT_ANALYZED));
				 doc.add(new Field("SUBJECT",message.getSubject(),Field.Store.YES,Field.Index.NOT_ANALYZED));
				 doc.add(new Field("BODY_MESSAGE",message.getBodyMessage(),Field.Store.YES,Field.Index.NOT_ANALYZED));
				 doc.add(new Field("DATE",dateWithPlainFormat,Field.Store.YES,Field.Index.NOT_ANALYZED));
				 doc.add(new Field("PATH",messagePath,Field.Store.YES,Field.Index.NOT_ANALYZED));
				 indexWriter.addDocument(doc);
			 }
			 catch (IOException e) {
				 System.err.println("Error: " + e);
			 }
			 indexWriter.optimize();
			 indexWriter.close();
		} catch (CorruptIndexException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (LockObtainFailedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

/**
 * This method is used to delete the index of the deleted mail based on its path
 * 	
 * @param message
 * @param path
 */
	
	public void deleteFileIndex(Message message,String path){
		
		File indexStorage=new File(INDEX_FOLDER);
		try {
				indexWriter = new IndexWriter(
					FSDirectory.open(indexStorage),
					new StandardAnalyzer(Version.LUCENE_CURRENT),
					false,
					IndexWriter.MaxFieldLength.LIMITED);
		
			String messagePath = path + File.separator + message.getId().toString();	
			Term term = new Term("PATH",messagePath);							
			Query query= new WildcardQuery(term);
			indexWriter.deleteDocuments(query);
			indexWriter.optimize();
			indexWriter.close();
			
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}//class



